{% extends "base.html" %}
{% block content %}
<h1 class="p-name p-summary">Your Search Results</h1>
  <form action="/results" method="GET" role="search">
    <input type="text" id="query" name="query" placeholder="Enter your query" aria-label="Search the IndieWeb" value="{{ query }}">
    <input class="search_button" type="submit" value="Search">
  </form>
  <p>Returned {{ '{0:,}'.format(number_of_results) }} search results for your "{{ query }}" query.</p>
  <p>
    {% if ordered_by == "date_asc" %}
    Ordered by date (ascending). You can also order by <a href="{{ base_results_query }}">relevance</a> or <a href="{{ base_results_query }}&order=date_desc">date (descending)</a>.
    {% elif ordered_by == "date_desc" %}
    Ordered by date (descending). You can also order by <a href="{{ base_results_query }}&order=date_asc">date (ascending)</a> or <a href="{{ base_results_query }}">relevance</a>.
    {% else %}
    Ordered by relevance. You can also order by <a href="{{ base_results_query }}&order=date_asc">date (ascending)</a> or <a href="{{ base_results_query }}&order=date_desc">date (descending)</a>.
    {% endif %}
  </p>
  {% if special_result == False %}<hr>{% endif %}
  {% if out_of_bounds_page == False %}
    {% include "search/special_serp_items.html" %}
    <ol class="serp">
      {% set count = 0 %}
      {% for r in results %}
      {% set count = count + 1 %}
      {% if r['_source']['url'] != special_result.breadcrumb %}
        <li>
          <p class="breadcrumbs"><a href="{% if count < 3 %}/go?query={{ query }}&page={{ page }}&r={% endif %}{{ r['_source']['url'] }}" style="border-bottom: none;">{% for item in r['_source']['url'].replace("https://", "").replace("http://", "").rstrip("/").split("/") %} {{ item.replace("%20", " ") }} {% if not loop.last %}>{% endif %}{% endfor %}</a></p>
          <p class="p-name"><a href="{% if count < 3 %}/go?query={{ query }}&page={{ page }}&r={% endif %}{{ r['_source']['url'] }}" class="u-url">{% if r['_source']['title']|length == 0 %}{{ " ".join(r['_source']['url'].split(" ")[:20]) }}{% if r['_source']['url'].split(" ")|length > 20 %}...{% endif %}{% else %}{{ " ".join(r['_source']['title'].split(" ")[:20]) }}{% if r['_source']['title'].split(" ")|length > 20 %}...{% endif %}{% endif %}</a></p>
          <p class="p-summary">{% if r['_source']['meta_description']|length > 0 %}{{ r['_source']['meta_description'] }}{% elif (r['_source']['h1'].split(",") and r['_source']['h1'].split(",")[0]|length > 0) or r['_source']['h1'] != "" %}{{ r['_source']['h1'].split(",")[0] }}{% else %}{{ r['_source']['h2'].split(",")[0] }}{% endif %}</p>
          {{ r['_source']['word_count'] }} | {{ r['_source']['incoming_links'] }}
          {% if r['_source']['url'].startswith("https://indieweb.org/") or r['_source']['url'].startswith("https://microformats.org/wiki/") %}
          {% set title_count = 0 %}
          <p>
            {% for header in r['_source']['h2'].split(", ")[:5] %}
              {% if header != "Navigation menu" and header != "See Also" %}
                <a href="{{ r['_source']['url'] }}#{{ header }}">{{ header }}</a>
                {% if header != r['_source']['h2'].split(", ")[:5][-1] %} | {% endif %}
              {% endif %}
            {% endfor %}
          </p>
          {% endif %}
        </li>
      <hr>
      {% endif %}
      {% endfor %}
    </ol>
  {% elif out_of_bounds_page == Truey %}
    <p>There are no results matching your query.</p>
    {% if suggestion_made and corrected_text.strip() != query.strip() %}
      <p>Did you mean <a href="/results?query={{ corrected_text.strip() }}">{{ corrected_text.strip() }}</a>?</p>
    {% else %}
      <p>Please make sure your spelling is correct and if it is, try searching for another term.</p>
    {% endif %}
  {% if page_count > 1 %}
    <ol class="tab">
      {% if page != 1 %}
      <li class="pages"><a href="{{ base_results_query }}&page={{ page -1 }}" rel="prev">Previous</a></li>
      {% endif %}
      {% for i in range(1, page_count+1) %}
        {% if i < 3 or i in [page-1, page, page+1] or i in [page_count-1, page_count] %}
          <li class="pages"><a href="{{ base_results_query }}&page={{ i }}" {% if i == page %}class="active_tab"{% endif %}>{{ i }}</a></li>
        {% endif %}
        {% if (i == page-2) or (i == page+2 and page+2 != page_count) %}
          <li>...</li>
        {% endif %}
      {% endfor %}
      {% if page < page_count %}
      <li class="pages"><a href="{{ base_results_query }}&page={{ page + 1 }}" rel="next">Next</a></li>
      {% endif %}
    </ol>
  {% endif %}
{% endblock %}